package com.hh.game.service.gateway.manager.notice;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hh.game.common.base.manager.notice.NoticeConstant;
import com.hh.game.common.base.manager.notice.NoticeConsumer;
import com.hh.game.common.base.manager.session.Session;
import com.hh.game.service.gateway.codec.websocket.OutputMessage;
import com.hh.game.service.gateway.manager.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
//@RocketMQListener(topic = NoticeConstant.TOPIC, messageModel = MessageModel.BROADCASTING)
public class NoticeConsumerImpl implements NoticeConsumer {
    private Logger log = LoggerFactory.getLogger(NoticeConsumerImpl.class);

//    @RocketMQMessage(messageClass = String.class, tag = NoticeConstant.TAG_NOTICE)
    public void onNoticeMessage(String msg) {
        log.info("on notice msg :" + msg);
        JSONObject json = JSON.parseObject(msg);
        acceptNotice(json.getIntValue(NoticeConstant.MESSAGE_RECEPTOR_KEY), json);
    }

//    @RocketMQMessage(messageClass = String.class, tag = NoticeConstant.TAG_BROADCAST)
    public void onBroadcastMessage(String msg) {
        log.info("on broadcast msg :" + msg);
        JSONObject json = JSON.parseObject(msg);
        acceptBroadcast(json);
    }

    @Override
    public void acceptNotice(int userId, Object data) {
        JSONObject obj = (JSONObject) data;
        String route =  obj.getString(NoticeConstant.MESSAGE_TO_CLIENT_ROUTE_KEY);
        if (!StrUtil.isBlank(route)) {
            String version = obj.getString(NoticeConstant.MESSAGE_TO_CLIENT_VERSION_KEY);
            Session session = SessionManager.getSession(userId);
            noticeUser(session, route, version, obj.get(NoticeConstant.MESSAGE_TO_CLIENT_DATA_KEY));
        } else {
            log.error("notice client msg cannot find route");
        }
    }

    @Override
    public void acceptBroadcast(Object data) {
        JSONObject obj = (JSONObject) data;
        String route = (String) obj.remove(NoticeConstant.MESSAGE_TO_CLIENT_ROUTE_KEY);
        if (!StrUtil.isBlank(route)) {
            String version = (String) obj.remove(NoticeConstant.MESSAGE_TO_CLIENT_VERSION_KEY);
            Collection<Session> sessions = SessionManager.getAllOnLine();
            for(Session session : sessions){
                noticeUser(session, route, version, obj);
            }
        } else {
            log.error("notice client msg cannot find route");
        }
    }

    private void noticeUser(Session session, String route, String version, Object data) {
        if (session != null && session.isOpen()) {
            OutputMessage msg;
            if (version == null) {
                msg = new OutputMessage(route, data);
            } else {
                msg = new OutputMessage(version, route, data);
            }
            session.channel().writeAndFlush(msg);
        }
    }
}
